package cn.kkcxy.blog.server.controller.client;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.kkcxy.blog.common.constant.ExceptionMessageConstant;
import cn.kkcxy.blog.core.dto.client.ClientArticleCategoryQueryParamsDTO;
import cn.kkcxy.blog.core.dto.client.ClientArticleTagsRequestDTO;
import cn.kkcxy.blog.core.result.ResponseEntity;
import cn.kkcxy.blog.core.vo.client.archive.ClientArticleArchiveVO;
import cn.kkcxy.blog.core.vo.client.archive.category.ClientArticleCategoryParentInfoVO;
import cn.kkcxy.blog.core.vo.client.archive.category.ClientArticleCategoryVO;
import cn.kkcxy.blog.core.vo.client.archive.tag.ClientArticleTagsVO;
import cn.kkcxy.blog.core.exception.article.ArticleServiceException;
import cn.kkcxy.blog.system.service.article.ArticleService;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @BelongsProject: kkcxy-blog
 * @BelongsPackage: cn.kkcxy.blog.server.controller.client
 * @Author: kkcxy
 * @CreateTime: 2024-09-18 00:39
 * @Description: Client/Article - controller
 * @Version: 1.0
 */
@Tag(name = "用户端-文章模块 Controller")
@Slf4j
@RestController
@RequestMapping("/client/article")
public class ArticleController {

    /**
     * Article Service
     */
    @Resource
    private ArticleService articleService;

    /**
     * @description: [用户端] 获取文章归档数据
     * @author: kkcxy
     * @date: 2024/9/18 1:57
     * @param: []
     * @return: cn.kkcxy.blog.core.result.ResponseEntity<cn.kkcxy.blog.core.vo.client.archive.ClientArticleArchiveVO>
     **/
    @SaIgnore
    @GetMapping("/archive")
    @Operation(summary = "[用户端] 获取文章归档数据")
    public ResponseEntity<List<ClientArticleArchiveVO>> getArticleArchiveData() {
        try {
            if (log.isInfoEnabled()) {
                log.info("[用户端] 获取文章归档数据");
            }
            return ResponseEntity.success(articleService.getArticleArchiveData());
        } catch (ArticleServiceException e) {
            log.error("[用户端] 获取文章归档数据 [ERROR]: {}", e.getMessage(), e);
            return ResponseEntity.fail(ExceptionMessageConstant.GET_ARTICLE_ARCHIVE_DATA_ERROR);
        }
    }

    /**
     * @description: [用户端] 获取文章分类列表数据
     * @author: kkcxy
     * @date: 2024/9/19 1:51
     * @param: [queryParamsDTO]
     * @return: cn.kkcxy.blog.core.result.ResponseEntity<java.util.List<cn.kkcxy.blog.core.vo.client.archive.category.ClientArticleCategoryVO>>
     **/
    @SaIgnore
    @GetMapping("/category")
    @Operation(summary = "[用户端] 获取文章分类列表数据")
    public ResponseEntity<ClientArticleCategoryVO> getArticleCategoryListData(ClientArticleCategoryQueryParamsDTO clientArticleCategoryQueryParamsDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("[用户端] 获取文章分类列表数据 请求参数: {}", JSON.toJSONString(clientArticleCategoryQueryParamsDTO));
            }
            return ResponseEntity.success(articleService.getArticleCategoryListData(clientArticleCategoryQueryParamsDTO));
        } catch (ArticleServiceException e) {
            log.error("[用户端] 获取文章分类列表数据 [ERROR]: {}", e.getMessage(), e);
            return ResponseEntity.fail(e.getMessage());
        }
    }

    /**
     * @description: [用户端] 获取父级分类信息根据分类名称
     * @author: kkcxy
     * @date: 2024/9/24 0:52
     * @param: [categoryName]
     * @return: cn.kkcxy.blog.core.result.ResponseEntity<cn.kkcxy.blog.core.vo.client.archive.category.ClientArticleCategoryParentInfoVO>
     **/
    @SaIgnore
    @GetMapping("/category/parent/info")
    @Operation(summary = "[用户端] 获取父级分类信息根据分类名称")
    public ResponseEntity<ClientArticleCategoryParentInfoVO> getArticleCategoryParentInfoByCategoryName(String categoryName) {
        try {
            if (log.isInfoEnabled()) {
                log.info("[用户端] 获取父级分类信息根据分类名称 请求参数: categoryName = {}", categoryName);
            }
            return ResponseEntity.success(articleService.getArticleCategoryParentInfoByCategoryName(categoryName));
        } catch (ArticleServiceException e) {
            log.error("[用户端] 获取父级分类信息根据分类名称 [ERROR]: {}", e.getMessage(), e);
            return ResponseEntity.fail(e.getMessage());
        }
    }

    /**
     * @description: [用户端] 获取文章标签列表数据
     * @author: kkcxy
     * @date: 2024/9/21 0:36
     * @param: [clientArticleTagsRequestDTO]
     * @return: cn.kkcxy.blog.core.result.ResponseEntity<cn.kkcxy.blog.core.vo.client.archive.tag.ClientArticleTagsVO>
     **/
    @SaIgnore
    @GetMapping("/tags")
    @Operation(summary = "[用户端] 获取文章标签列表数据")
    public ResponseEntity<ClientArticleTagsVO> getArticleTagsListOrArticleList(ClientArticleTagsRequestDTO clientArticleTagsRequestDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("[用户端] 获取文章标签列表数据 请求参数: {}", JSON.toJSONString(clientArticleTagsRequestDTO));
            }
            return ResponseEntity.success(articleService.getArticleTagsListOrArticleList(clientArticleTagsRequestDTO));
        } catch (ArticleServiceException e) {
            log.error("[用户端] 获取文章标签列表数据 [ERROR]: {}", e.getMessage(), e);
            return ResponseEntity.fail(e.getMessage());
        }
    }

}